﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Data.SqlClient;

public partial class Quantringuoidung_Member : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Admin"] == null)
        {
            if (Session["Employee"] != null)
            {
                Response.Redirect("Messaging.aspx");
            }
            Response.Redirect("../login.aspx");

        }
    }
    protected void RadGrid_user_ItemDeleted(object sender, Telerik.Web.UI.GridDeletedEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        String id = dataItem.GetDataKeyValue("Username").ToString();

        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            SetMessage("Admin with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("Admin with ID " + id + " is deleted!");
        }
    }
    private void DisplayMessage(string text)
    {
        RadGrid_user.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
    }

    private void SetMessage(string message)
    {
        gridMessage = message;
    }

    private string gridMessage = null;

    private void DisplayMessage(bool isError, string text)
    {
        Label label = (isError) ? this.Label1 : this.Label2;
        label.Text = text;
    }
    private void insert(string User, string Pass, string name,string dchi,string email,string phone,DateTime ns,bool isadmin )
    {
        SqlConnection con = new SqlConnection("Data Source=C6E9961739FB407;Initial Catalog=QLMT;Integrated Security=True");
        //SqlConnection con = new SqlConnection(Connection.);
        string sqlStatement = string.Empty;

        sqlStatement = "INSERT INTO Admin1" +
                            "(Username, Password,FullName,Address, Email,Phone,Birthday,IsAdmin)" +
                            "VALUES (@Username, @Password,@FullName,@Address, @Email,@Phone,@Birthday,@IsAdmin)";

        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(sqlStatement, con);
            cmd.Parameters.AddWithValue("@Username", User);
            cmd.Parameters.AddWithValue("@Password", Pass);
            cmd.Parameters.AddWithValue("@FullName", name);
            cmd.Parameters.AddWithValue("@Address", dchi);
            cmd.Parameters.AddWithValue("@Email", email);
            cmd.Parameters.AddWithValue("@Phone", phone);
            cmd.Parameters.AddWithValue("@Birthday", ns);
            cmd.Parameters.AddWithValue("@IsAdmin", isadmin);


            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            con.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //DateTime date = DateTime.ParseExact(RadDatePicker_nsinh.SelectedDate,"dd/MM/yyyy",null);
        insert(txtuser.Text, txtPassword.Text, txtname.Text, txtAD.Text, txtemail.Text, txtphone.Text,RadDatePicker_nsinh.SelectedDate.Value, checkIsadmin.Checked);
    }
    protected void refesh_Click(object sender, EventArgs e)
    {
        sql_user.SelectCommand = "Select * from Admin1";

    }
}
